package com.test.java;

/**
 * 路径
 * =============================================================
 * Version          Author          Date            Description
 * 1.0              白建         2017/12/5             创建
 * =============================================================
 */
public class Java_1
{

    private static int DIMENSION = 5;

    private static int[][] MATRIX;

    private static byte[][] PATH;

    public static void main(String[] args)
    {

        init();
        path();
        new Java_0().showMatrix(MATRIX, DIMENSION, DIMENSION);
    }

    private static void init()
    {

        MATRIX = new int[DIMENSION][DIMENSION];
        PATH = new byte[DIMENSION][DIMENSION];
        //设置路障
        MATRIX[0][DIMENSION - 1] = -1;
        MATRIX[0][DIMENSION - 2] = -1;
        MATRIX[DIMENSION - 1][DIMENSION - 1] = -1;
        MATRIX[DIMENSION - 1][DIMENSION - 2] = -1;
    }

    private static void path()
    {

        int chessman = 0;
        int i = 0, j = 0;
        MATRIX[i][j] = ++chessman;
        while (true)
        {
            int flag = hasNext(i, j);
            if (flag == 0)
            {
                break;
            }
            else
            {
                switch (flag)
                {
                case 1:
                    MATRIX[i][j + 1] = ++chessman;
                    PATH[i][j + 1] = 1;
                    j++;
                    break;
                case 2:
                    MATRIX[i + 1][j] = ++chessman;
                    PATH[i + 1][j] = 1;
                    i++;
                    break;
                case 3:
                    MATRIX[i][j - 1] = ++chessman;
                    PATH[i][j - 1] = 1;
                    j--;
                    break;
                case 4:
                    MATRIX[i - 1][j] = ++chessman;
                    PATH[i - 1][j] = 1;
                    i--;
                    break;
                case 5:
                    PATH[i][j + 1] = 1;
                    j++;
                    break;
                case 6:
                    PATH[i + 1][j] = 1;
                    i++;
                    break;
                case 7:
                    PATH[i][j - 1] = 1;
                    j--;
                    break;
                case 8:
                    PATH[i - 1][j] = 1;
                    i--;
                    break;
                default:
                    break;
                }
            }
        }
    }

    private static int hasNext(int i, int j)
    {

        if (!overStep(i, j + 1) && !walked(i, j + 1) && MATRIX[i][j + 1] == -1)
        {
            return 5;
        }
        if (!overStep(i, j + 1) && !walked(i, j + 1) && MATRIX[i][j + 1] == 0)
        {
            return 1;
        }
        if (!overStep(i + 1, j) && !walked(i + 1, j) && MATRIX[i + 1][j] == -1)
        {
            return 6;
        }
        if (!overStep(i + 1, j) && !walked(i + 1, j) && MATRIX[i + 1][j] == 0)
        {
            return 2;
        }
        if (!overStep(i, j - 1) && !walked(i, j - 1) && MATRIX[i][j - 1] == -1)
        {
            return 7;
        }
        if (!overStep(i, j - 1) && !walked(i, j - 1) && MATRIX[i][j - 1] == 0)
        {
            return 3;
        }
        if (!overStep(i - 1, j) && !walked(i - 1, j) && MATRIX[i - 1][j] == -1)
        {
            return 8;
        }
        if (!overStep(i - 1, j) && !walked(i - 1, j) && MATRIX[i - 1][j] == 0)
        {
            return 4;
        }
        return 0;
    }

    private static boolean overStep(int i, int j)
    {

        return i > 4 || j > 4 || i < 0 || j < 0;
    }

    private static boolean walked(int i, int j)
    {

        return PATH[i][j] == 1;
    }
}
